package vcp.server.requesthandler;

import ocsf.server.ConnectionToClient;
import vcp.client.request.PeriodicReportSaveRequest;
import vcp.client.request.response.ResponseRequest;
import vcp.entity.PeriodicReport;
import vcp.server.DAO.PeriodicReportDAO;
import vcp.server.interfaces.IServer;
import vcp.server.utility.Constants.TableNames;

/**
 * 
 * @author VCP team
 *         <p>
 *         Responsible for saving periodic reports
 *         </p>
 */
public class PeriodicReportSaveRequestHandler extends RequestHandler<PeriodicReportSaveRequest>
{
	/** saving periodic reports constructor **/
	public PeriodicReportSaveRequestHandler(IServer server)
	{
		super(server);
	}

	@Override
	public void execute(PeriodicReportSaveRequest request, ConnectionToClient client)
	{
		PeriodicReportDAO periodicReportDAO = new PeriodicReportDAO();

		// retrieves all relevant info from request
		PeriodicReport periodicReport = periodicReportDAO.create();
		periodicReport.setID(this.server.getGeneratedID(TableNames.PERIODIC_REPORT));
		periodicReport.setType(request.getForm().getReportType());
		periodicReport.setSite(request.getForm().getSiteEnum());
		periodicReport.setStartDate(request.getForm().getStartDate());
		periodicReport.setEndDate(request.getForm().getEndDate());

		// save the periodic report
		periodicReportDAO.save(periodicReport);

		// response client, that periodic report was saved
		this.sendResponse(new ResponseRequest(request.getRequestID(), new Boolean(true)), client);
	}

}
